package leetcode;

/**
 * @author yinlei
 * @date 2018/6/26
 * @since 1.0.0
 */
public class N849MaximizeDistanceToClosestPerson {

    public static void main(String[] args) {

        System.out.println(maxDistToClosest(new int[]{1,0,0,0,1,0,1}));
        System.out.println(maxDistToClosest(new int[]{1,0,0,0}));
        System.out.println(maxDistToClosest(new int[]{0,0,1,0}));
        System.out.println(maxDistToClosest(new int[]{1,1,0,0,0,0,1}));
        System.out.println(maxDistToClosest(new int[]{0,1}));
        System.out.println(maxDistToClosest(new int[]{0, 0,1}));
        System.out.println(maxDistToClosest(new int[]{0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0}));
        System.out.println(maxDistToClosest(new int[]{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}));
        System.out.println(maxDistToClosest(new int[]{1,1,1,0,0,1,1,1,0,0,0,1,0,0,0,1,0,1,1}));
    }

    static public int maxDistToClosest(int[] seats) {

        int offset = -1;
        int count = 0;
        int max = 0;

        for (int i = 0; i < seats.length; i++) {
            if (seats[i] == 1) {
                if (offset == -1) {
                    max = count;
                } else {
                    int l = (i - offset) / 2;
                    if (l > max) {
                        max = l;
                    }
                }
                offset = i;
                count = 0;
            } else {
                count++;
            }
        }

        if (seats[seats.length - 1] == 0 && count > max) {
            max = count;
        }
        return max;
    }
}
